/// <summary>
// CodeFx.MySqlProviders - .NET Providers for MySql using the framework CodeFx
// Laurent Morisseau
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Laurent Morisseau
/// </summary>
/// <author><a href="mailto:laurent.morisseau@gmail.com">Laurent Morisseau</a></author>


namespace CodeFx.MySqlProviders
{
    using System;
    using System.Text;
    using System.Web;
    using System.Data;
    using System.ComponentModel;
    using MySql.Data.MySqlClient;
    using System.Collections;
    using CodeFx;
    using System.Collections.Specialized;
    using CodeFx.Web;

    public static partial class Aspnet_rolesDTO
	{
        internal static StringCollection UsersInRoles_GetRolesForUser(uint ApplicationId, uint UserId)
        {
            const string command = "SELECT r.RoleName FROM aspnet_roles r inner join aspnet_usersinroles ur on (r.RoleId = ur.RoleId AND ur.UserId = ?UserId) where r.ApplicationId = ?ApplicationId ORDER BY r.RoleName";

            MySqlConnection conn = ConnectionString.Current.OpenConnection;       
			MySqlCommand cmd = new MySqlCommand(command, conn);
            cmd.Parameters.Add(new MySqlParameter("?UserId", UserId));
            cmd.Parameters.Add(new MySqlParameter("?ApplicationId", ApplicationId));
            StringCollection al = null;
            MySqlDataReader dr = null;
            try
            {
                dr = cmd.ExecuteReader();
                if (dr.HasRows)
                    al = new StringCollection();
                while (dr.Read())
                {
                    al.Add(dr.GetString(0));
                }
            }
            catch (Exception ex)
            {
                throw new DALException(cmd.CommandText, ex.InnerException);
            }
            finally
            {
                if (dr != null)
                    dr.Close();
                conn.Close();
            }
            return al;
        }

        internal static void DeleteByFk_ApplicationId_RoleId(Aspnet_rolesDAL dataObject)
        {
            if (dataObject == null)
                return;

            const string command = "delete s from aspnet_roles s   where s.ApplicationId =?ApplicationId and s.RoleId =?RoleId";

            MySqlConnection conn = ConnectionString.Current.OpenConnection;       
            MySqlCommand cmd = new MySqlCommand(command, conn);

            cmd.Parameters.Add(new MySqlParameter("?ApplicationId", dataObject.ApplicationId));
            cmd.Parameters.Add(new MySqlParameter("?RoleId", dataObject.RoleId));

            try
            {
                cmd.ExecuteNonQuery();
            }
            finally
            {
                conn.Close();
            }
        }

        internal static uint GetRoleId(string _RoleName, uint _ApplicationId)
        {

            uint roleid=0;
            string command = "select RoleId from aspnet_roles where RoleName =?RoleName and ApplicationId =?ApplicationId ";

            MySqlConnection conn = ConnectionString.Current.OpenConnection;       
            MySqlCommand cmd = new MySqlCommand(command, conn);

            cmd.Parameters.Add(new MySqlParameter("?RoleName", _RoleName));
            cmd.Parameters.Add(new MySqlParameter("?ApplicationId", _ApplicationId));
            MySqlDataReader dr = null;
            try
            {
                dr = cmd.ExecuteReader();
                if (dr.Read())
                    roleid = dr.GetUInt32(0);
            }
            catch (Exception ex)
            {
                throw new DALException(cmd.CommandText, ex.InnerException);
            }
            finally
            {
                if (dr != null)
                    dr.Close();
                conn.Close();
            }
            return roleid;

        }

        internal static bool Exist(string RoleName)
        {
            bool exist;
            string command = "select 1 from aspnet_roles where RoleName =?RoleName";

            MySqlConnection conn = ConnectionString.Current.OpenConnection;       
            MySqlCommand cmd = new MySqlCommand(command, conn);

            cmd.Parameters.Add(new MySqlParameter("?RoleName", RoleName));
            MySqlDataReader dr = null;
            try
            {
                dr = cmd.ExecuteReader();
                exist = dr.Read();
            }
            catch (Exception ex)
            {
                throw new DALException(cmd.CommandText, ex.InnerException);
            }
            finally
            {
                if (dr != null)
                    dr.Close();
                conn.Close();
            }
            return exist;
        }
	
	}
}